package com.int_tech.timeTask194;

import com.alibaba.fastjson.JSON;
import com.int_tech.messionLog.dao.MessionLogShowDao;
import com.int_tech.messionLog.entity.MessionLogShow;
import com.int_tech.utils.DateTool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/10/15.
 */
@Component
public class TimetaskTest {

    @Autowired
    private MessionLogShowDao messionLogShowDao;

    @Scheduled(cron = "0 0/5 * * * ?")
    public void getThisMonthMession() throws ParseException, IOException {

        if(true)
        {

            String date = "2018-10-10 00:00:00";

//            String date2 = "2018-10-21 00:00:00";

            Date d = DateTool.string2Date(date,"yyyy-MM-dd HH:mm:ss");
//            Date d2 = DateTool.string2Date(date2,"yyyy-MM-dd HH:mm:ss");

            long s1 = System.currentTimeMillis();
            //分页查询
            PageRequest pageRequest = new PageRequest(0, 10000);
            Page<MessionLogShow> messionLogShowPage = messionLogShowDao.findByMessionCountAndCreateTimeAfter(pageRequest,"0",d);
            List<MessionLogShow> messionLogShowList = messionLogShowPage.getContent();
            long s2 = System.currentTimeMillis();
            System.out.print("查询数据耗时" + (s2 - s1) + "ms");

            int m = 0;

            for(MessionLogShow messionLogShow : messionLogShowList)
            {
                messionLogShow.setMessionCount("1");
                messionLogShowDao.save(messionLogShow);
                if(m%10000 == 0)
                {
                    System.out.print("一共"+messionLogShowList.size()+"已经到第"+m+"个了");
                }
                m++;
                String messionNumber = messionLogShow.getMessionNumber();
                String carNumber = messionLogShow.getCarNumber();

//            List<MessionLogShow> a = (List<MessionLogShow>) memCachedClient.get(messionNumber+"_"+carNumber);
                String thisMonth = DateTool.Date2String(new Date(),"yyyyMM");
                String b = getMem(thisMonth+"_"+messionNumber+"_"+carNumber);

//            String result =

                if(StringUtils.isEmpty(b))
                {
                    List<MessionLogShow> a = new ArrayList<>();
                    a.add(messionLogShow);
                    setMem(messionNumber+"_"+carNumber, JSON.toJSONString(a));
                }
                else
                {
//                String result = b.toString();

                    try
                    {
                        List<MessionLogShow> a = JSON.parseArray(b, MessionLogShow.class);
                        a.add(messionLogShow);
                        setMem(messionNumber+"_"+carNumber, JSON.toJSONString(a));
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                        List<MessionLogShow> a = new ArrayList<>();
                        a.add(messionLogShow);
                        setMem(messionNumber+"_"+carNumber, JSON.toJSONString(a));
                    }

                }
            }

            long s3 = System.currentTimeMillis();
            System.out.print("存储缓存数据耗时" + (s3 - s2) + "ms");

        }
    }


    public void setMem(String key,String value){
        try {
            String host = "r-hp3bed87d9e50ed4.redis.huhehaote.rds.aliyuncs.com";//控制台显示访问地址
            int port = 6379;
            Jedis jedis = new Jedis(host, port);
            //鉴权信息
            jedis.auth("Inttech168");//password
            //select db默认为0
            jedis.select(1);
            //set一个key

            String thisMonth = DateTool.Date2String(new Date(),"yyyyMM");
            key = thisMonth + "_" + key;
            jedis.set(key, value);
//            System.out.println("Set Key " + key + " Value: " + value);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getMem(String key){
        String getvalue = "";
        try {
            String host = "r-hp3bed87d9e50ed4.redis.huhehaote.rds.aliyuncs.com";//控制台显示访问地址
            int port = 6379;
            Jedis jedis = new Jedis(host, port);
            //鉴权信息
            jedis.auth("Inttech168");//password
            //select db默认为0
            jedis.select(1);
            //set一个key
//            jedis.set(key, value);
//            System.out.println("Set Key " + key + " Value: " + value);
            //get 设置进去的key
            getvalue = jedis.get(key);
//            System.out.println("Get Key " + key + " ReturnValue: " + getvalue);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return getvalue;
    }


}
